﻿
Partial Class WebForm_ManageEcolife_SearchEcolifeID
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            'RaiseEvent Loaded(Me, New EventArgs)
            Me.VolunteerQueryControl1.Display_EcolifeID(True)
        End If
    End Sub

    Protected Sub btnQuery_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnQuery.Click
        Me.BindGV()
    End Sub

    Protected Sub gv_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gv.PageIndexChanging
        Me.gv.PageIndex = e.NewPageIndex
        Me.BindGV()
    End Sub

    Protected Sub gv_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles gv.Sorting
        Me.BindGV()
    End Sub

    Public Sub BindGV()
        Dim dat As New clsData
        Dim sql As New MyStringBuilder
        Dim dt As Data.DataTable

        sql.AppendFormat(" select v.* ")
        sql.AppendFormat(",oo.VOrganizationName ")
        sql.AppendFormat(",oo.VOrganizationID ")
        sql.AppendFormat(",oo.DirectUOrganizationID ")
        sql.AppendFormat(" from VolunteerDistinctView v ")
        'sql.AppendFormat(" inner join Village r on v.VillageID = r.RegionID ")

        sql.AppendFormat("  left JOIN (select vm.VOrganizationID,vo.VOrganizationName,vm.VolunteerID,vo.DirectUOrganizationID from VOrganizationMapping vm ")
        sql.AppendFormat("inner join VOrganizationViewV2 vo on vm.VOrganizationID=vo.VOrganizationID ) oo on v.VolunteerID=oo.VolunteerID  ")

        sql.AppendFormat(" where 1=1 ")
        sql.AppendFormat(" and v.EcolifeID<>'' ")

        sql.AppendFormat(GetWhereSQL())

        sql.AppendFormat(Me.gv.OrderBySQL)

        dt = dat.GetData(sql)

        Me.gv.DataSource = dt
        Me.gv.DataBind()
    End Sub

    Private Function GetWhereSQL() As String
        Dim sql As New MyStringBuilder

        If Me.VolunteerQueryControl1.VolunteerId.Trim() = "" Then
            If VolunteerQueryControl1.OrganizationID <> "0" Then
                sql.AppendFormat("AND  oo.DirectUOrganizationID ='{0}'   ", VolunteerQueryControl1.OrganizationID)
            End If
            If VolunteerQueryControl1.VOrganizationID <> "0" Then
                sql.AppendFormat("AND oo.VOrganizationID={0}  ", VolunteerQueryControl1.VOrganizationID)
            End If
            'sql.AppendFormat(" and (Left(v.VillageID,3) like '%{0}%' or ''='{0}')", Me.VolunteerQueryControl1.Region)
            'sql.AppendFormat(" and (v.VillageID='{0}' or ''='{0}')", Me.VolunteerQueryControl1.Region2)
            sql.AppendFormat(" and (v.VolunteerName like '%{0}%' or ''='{0}')", Me.VolunteerQueryControl1.VolunteerName.Trim())
            sql.AppendFormat("AND (oo.VOrganizationID = {0} or {0}=0 ) ", Me.VolunteerQueryControl1.VOrganizationID)
            'sql.AppendFormat(" AND (v.VolunteerID IN (SELECT distinct VolunteerID FROM VOrganizationMapping WHERE VOrganizationID={0}  ) or '{0}' =0 ) ", Me.VolunteerQueryControl1.VOrganizationID)
        Else
            sql.AppendFormat(" and (v.VolunteerID='{0}' or ''='{0}')", Me.VolunteerQueryControl1.VolunteerId.Trim())
        End If

        If Me.VolunteerQueryControl1.EcolifeID.Trim() <> "" Then
            sql.AppendFormat(" and v.EcolifeID='{0}' ", Me.VolunteerQueryControl1.EcolifeID.Trim())
        End If

        Return sql.ToString()
    End Function

    Protected Sub gv_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gv.RowCommand
        If e.CommandName = "Select" Then
            Dim row As GridViewRow = TryCast(TryCast(e.CommandSource, Control).NamingContainer, GridViewRow)
            Me.EcolifeID = Me.gv.DataKeys(row.RowIndex).Item("EcolifeID").ToString()
            RaiseEvent SelectedIndexChanged(Me, New EventArgs)
        End If
    End Sub

    Public Property EcolifeID() As String
        Get
            Return ViewState("EcolifeID")
        End Get
        Set(ByVal value As String)
            ViewState("EcolifeID") = value
        End Set
    End Property

    Public Event Loaded(ByVal source As Object, ByVal e As EventArgs)
    Public Event SelectedIndexChanged(ByVal source As Object, ByVal e As EventArgs)
End Class
